{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import twitter\n",
    "consumer_key = \"59tyrGqNHCdGB92eFYEsqcjdg\"\n",
    "consumer_secret = \"DhglQERO5u936QibJP8YLbu6w60zmrxzl7jM0KmTQZZ0AXhr10\"\n",
    "access_token = \"16065520-USf3DBbQAh6ZA8CnSAi6NAUlkorXdppRXpC4cQCKk\"\n",
    "access_token_secret = \"DowMQeXqh5ZsGvZGrmUmkI0iCmI34ShFzKF3iOdiilpX5\"\n",
    "authorization = twitter.OAuth(access_token, access_token_secret, consumer_key, consumer_secret)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/home/robert/data/datasets/twitter/python_tweets.json\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "output_filename = os.path.join(os.path.expanduser(\"~\"), \"data\", \"datasets\", \"twitter\", \"python_tweets.json\")\n",
    "print(output_filename)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "t = twitter.Twitter(auth=authorization)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Saved 100 entries\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "\n",
    "n_output = 0\n",
    "\n",
    "with open(output_filename, 'a') as output_file:\n",
    "    search_results = t.search.tweets(q=\"python\", count=100)['statuses']\n",
    "    for tweet in search_results:\n",
    "        if 'text' in tweet:\n",
    "            output_file.write(json.dumps(tweet))\n",
    "            output_file.write(\"\\n\\n\")\n",
    "            n_output += 1\n",
    "\n",
    "print(\"Saved {} entries\".format(n_output))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
